#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :test.py
# @Time      :2023/12/25 
# @Author    :CL
# @email     :1037654919@qq.com
# 保存ppt信息到数据库，并且根据信息下载ppt内容，维护数据库
# 官网 https://www.islide.cc/ppt
import time
import requests
from retrying import retry
import logging
from utils import mongo_manager, get_proxy


islide_data = mongo_manager('islide_data', db='public_data')

headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN",
    "App-Language": "zh-CN",
    "Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6eyJhY2NvdW50SWQiOjY4MDY1NDAsImxvZ2luVHlwZSI6InBob25lIiwibmFtZSI6NTI5NDIzOX0sImlzcyI6Ik1JTkciLCJleHAiOjE3MDYwNjM4NTQsImlhdCI6MTcwMzQ3MTg1NH0.Tt4ch7XpWO3UQPvdj72bw_chtQMQwIOgNVjKVxKyyA4",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Origin": "https://www.islide.cc",
    "Pragma": "no-cache",
    "Referer": "https://www.islide.cc/ppt/template?filters=&group=all&page=2",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
    "deviceid": "1703231986326721",
    "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Linux\"",
    "source": "web"
}


@retry(stop_max_attempt_number=3, wait_fixed=2000)
def get_info(id='801459'):
    url = "https://store-api.islide.cc/web/resources/id"
    params = {
        "id": id
    }
    response = requests.get(url, headers=headers, params=params, proxies=get_proxy(), timeout=10)
    print(response.url, response)
    if response.status_code == 200:
        return response.json()['data']


@retry(stop_max_attempt_number=3, wait_fixed=2000)
def get_list(url="https://store-api.islide.cc/web/resources/special/list/75?size=40&start=0"):
    response = requests.get(url, headers=headers, proxies=get_proxy(), timeout=10)
    requests.session().close()
    print(response.url, response)
    if response.status_code == 200:
        return response.json()


@retry(stop_max_attempt_number=3, wait_fixed=2000)
def get_all_list(params=None):
    url = "https://store-api.islide.cc/web/resources/list"
    if params is None:
        params = {
            "type": "theme",  # template  diagram smartdiagram
            "keywords": "",
            "filters": "",
            "group": "all",
            "size": "28",
            "start": "0"  # +=28
        }
    response = requests.get(url, headers=headers, params=params, proxies=get_proxy())
    print(response.url, response)
    if response.status_code == 200:
        return response.json()['data']['records']


def get_page():
    url = "https://store-api.islide.cc/web/special/page"
    params = {
        "size": "40",
        "start": "0"
    }
    response = requests.get(url, headers=headers, params=params)
    print(response.url, response)
    return response.json()


def save_data():  # 热门专题
    #    # save page
    # lists = get_page()['data']['records']
    # print(len(lists))
    # for ll in lists:
    #     ll['type'] = 'page'
    #     islide_data.insertOne(ll)
    # save ppt
    seeds = islide_data.findAll({'type': 'page', 'status': None})
    for seed in seeds:
        id = seed['id']
        start = 0
        while True:
            url = f"https://store-api.islide.cc/web/resources/special/list/{id}?size=40&start={start}"
            records = get_list(url=url)['data']['records']
            for record in records:
                ppt_id = record['id']
                data = get_info(id=ppt_id)
                try:
                    data["_id"] = data['uniqueId']
                    islide_data.insertOne(data)
                except Exception as e:
                    print(e)
            if len(records) == 0:
                break
            start += 40
        seed['status'] = 'done'
        islide_data.updateOne({'_id': seed['_id']}, seed)


def save_data2():  # ppt info to mongodb
    theme = ['theme']  # ,template 6356 'theme' 4060, 'diagram',2016 'smartdiagram' 7112 ,数字是对应的start，因为已经跑过了一些数据
    for t in theme:
        start = 0
        while True:

            params = {
                "type": t,  # template theme diagram smartdiagram
                "keywords": "",
                "filters": "",
                "group": "all",
                "size": "28",
                "start": start  # +=28
            }
            print(params)
            records = get_all_list(params=params)
            for record in records:
                ppt_id = record['id']
                data = get_info(id=ppt_id)
                try:
                    data["_id"] = data['uniqueId']
                    islide_data.insertOne(data)
                except Exception as e:
                    print(e)
            if len(records) < 28:
                break
            start += 28
            time.sleep(10)


if __name__ == "__main__":
    print()
    # print(get_list())
    # save_data()
    save_data2()
    islide_data.close()
